package com.txl.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

@RestController
public class FeelFishController {
    private static Date NOW_DATE = new Date();
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat showSdf = new SimpleDateFormat("M月d日");
    private static SimpleDateFormat timeSdf = new SimpleDateFormat("HH");
    private static SimpleDateFormat sdfiTmes = new SimpleDateFormat("yyyyMMddHHmmss");
    private static SimpleDateFormat yaer = new SimpleDateFormat("YYYY");
    private static SimpleDateFormat month = new SimpleDateFormat("MM");
    private static SimpleDateFormat day = new SimpleDateFormat("dd");

    /**
     * 摸鱼办提醒您：
     * 10月27日下午好，摸鱼人！工作再累 一定不要忘记摸鱼哦！
     * 有事没事起身去茶水间去厕所去廊道走走别老在工位上坐着
     * 钱是老板的 但命是自己的
     * 距离周末假期结束还有3天
     * 距离元旦假期还有66天
     * 距离春节假期还有97天
     * 放假不易，且行且珍惜！
     */
    @GetMapping("/moyu")
    public String test1() throws ParseException {
//        System.out.println("摸鱼办提醒您：");
        Date closeDate = sdfiTmes.parse(yaer.format(NOW_DATE) + month.format(NOW_DATE) + day.format(NOW_DATE) + "180000");
//        System.out.println(NOW_DATE.getTime() < closeDate.getTime() ? "【您的下班时间是17:30,距离您下班还有" + getEndWrokTimes(closeDate) + "】" : "【您正在加班！建议立即关机！】");
        String day = showSdf.format(NOW_DATE);  // 今日日期 => 10月27日
//        System.out.println(day + getTimeText() + ",摸鱼人！工作再累 一定不要忘记摸鱼哦！");
//        System.out.println("有事没事起身去茶水间去厕所去廊道走走别老在工位上坐着");
//        System.out.println("钱是老板的 但命是自己的");
//        System.out.println(zm() == 0 ? "顶住今天！明天就是周六，加油！打工人！" : "距离周末假期还有" + zm() + "天");
//        System.out.println("距离元旦假期还有" + getDistanceOfTwoDate(sdf.parse("2022-01-01"), sdf.parse(sdf.format(NOW_DATE))) + "天");
//        System.out.println("距离春节假期还有" + getDistanceOfTwoDate(sdf.parse("2022-02-01"), sdf.parse(sdf.format(NOW_DATE))) + "天");
        StringBuilder sb = new StringBuilder();
        sb.append("<!DOCTYPE html>");
        sb.append("<html>");
        sb.append("<head>");
        sb.append("<meta charset=\"utf-8\">");
        sb.append("<title>摸鱼办公室</title>");
        sb.append("</head>");
        sb.append("<body>");
        sb.append("<h1>摸鱼办提醒您：</h1>");
        sb.append(NOW_DATE.getTime() < closeDate.getTime() ? "<h3>您的下班时间是【18:00】,距离您下班还有【" + getEndWrokTimes(closeDate) + "】</h3>" : "<h3>【您正在加班！建议立即关机！】</h3>");
        sb.append("<p>" + day + getTimeText() + ",摸鱼人！工作再累 一定不要忘记摸鱼哦！" + "</p>");
        sb.append("<p>有事没事起身去茶水间去厕所去廊道走走别老在工位上坐着</p>");
        sb.append("<p>钱是老板的 但命是自己的</p>");
        sb.append(zm() == 0 ? "<p>顶住今天！明天就是周六，加油！打工人！</p>" : "<p>距离周末假期还有" + zm() + "天</p>");
        sb.append("<p>距离元旦假期还有" + getDistanceOfTwoDate(sdf.parse("2025-01-01"), sdf.parse(sdf.format(NOW_DATE))) + "天</p>");
        sb.append("<p>距离春节假期还有" + getDistanceOfTwoDate(sdf.parse("2025-01-29"), sdf.parse(sdf.format(NOW_DATE))) + "天</p>");
        sb.append("<p>放假不易，且行且珍惜！</p>");
        sb.append("</body>");
        sb.append("</html>");
        return sb.toString();
    }


    /**
     * 获取两个日期之间的天数
     *
     * @param before
     * @param after
     * @return
     */
    public static int getDistanceOfTwoDate(Date before, Date after) {
        long beforeTime = before.getTime();
        long afterTime = after.getTime();
        Long intervalDay = (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
        return Math.abs(intervalDay.intValue());
    }


    /**
     * 问候语
     *
     * @return
     */
    private static String getTimeText() {
        String time;
        if (belongCalendar(0, 6)) {
            time = "凌晨了";
        } else if (belongCalendar(6, 11)) {
            time = "早上好";
        } else if (belongCalendar(11, 14)) {
            time = "中午好";
        } else if (belongCalendar(14, 19)) {
            time = "下午好";
        } else if (belongCalendar(19, 24)) {
            time = "晚上好";
        } else {
            time = "你好";
        }
        return time;
    }

    /**
     * 判断是否在时间范围内
     *
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @return 范围内？真：假
     */
    private static boolean belongCalendar(int beginTime, int endTime) {
        return (Integer.parseInt(timeSdf.format(NOW_DATE)) > beginTime && Integer.parseInt(timeSdf.format(NOW_DATE)) <= endTime);
    }

    private static int zm() {
        int days = 0;
        Calendar calendar = new GregorianCalendar();
        for (int i = 1; ; i++) {
            calendar.add(calendar.DATE, 1);
            if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                days = --i;
                break;
            }
        }
        return days;
    }

    /**
     * @return 输出下班时分秒
     */
    private static String getEndWrokTimes(Date endDate) {
        Date nowDate = new Date();
        long time = endDate.getTime() - nowDate.getTime();
        // 时
        int hour = Math.round(time / 1000 / 60 / 60 % 24);
        // 分
        int minute = Math.round(time / 1000 / 60 % 60);
        // 秒
        int second = Math.round(time / 1000 % 60);
        return String.format("%s时%s分%s秒", hour, minute, second);

    }
}
